package com.emep.changzhi.analyse.http.basics;

import com.emep.changzhi.analyse.entity.basics.EPA;
import com.emep.changzhi.analyse.entity.basics.SysCompanyInfo;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.SysCompanyInfoService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import com.emep.changzhi.analyse.utils.MapToObject;
import com.emep.changzhi.analyse.utils.PageChunk;
import io.swagger.annotations.*;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Map;

/**
 * @author Wangcc
 * @version V1.0
 * @Description: 企业信息控制器
 * @date 2018/2/27 15:59
 */

@RestController
@RequestMapping("company")
@Api(value="企业controller",tags={"企业信息"})
public class SysCompanyInfoController {
    
    @Resource
    private SysCompanyInfoService service;

    MapToObject<SysCompanyInfo> mapToObject=new MapToObject<>();
    /**
     * 查询所有 企业信息
     * @return
     */
    @ApiOperation(value="获取用户列表", notes="无参数获取所有用户信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @PostMapping(value = "list")
    public ResultBean<Collection<SysCompanyInfo>> queryUser(){
        return new ResultBean<Collection<SysCompanyInfo>>(service.list());
    }

    /**
     * 新增企业信息
     * @param payload
     * @return
     */
    @ApiOperation(value="新增企业信息", notes="新增企业信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改时传的id", required = false, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "companyName", value = "企业名字", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "companyShortname", value = "企业简称", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "companyAddress", value = "企业地址", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "companyType", value = "单位性质标识", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "longitude", value = "经度", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "latitude", value = "纬度", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "epName", value = "环保联系人", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "epPhone", value = "环保手机号", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "epEmail", value = "环保邮箱", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "legalPersonName", value = "法人名字", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "legalPersonPhone", value = "法人手机", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "legalPersonFax", value = "法人传真", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "legalPersonEmail", value = "法人邮箱", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "buildDate", value = "建厂时间", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "productionDate", value = "投产时间", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "shutdownDate", value = "停产时间", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "organizationCode", value = "组织机构代码", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "runningState", value = "标识企业运营状态", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "isElectricityKw", value = "是否30万千万以上的企业", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "organizationForm", value = "组织形式", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "production", value = "主要产品", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "annualOutput", value = "年产量", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "SysRegionInfoId", value = "流域id", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "SysKeyTypeInfoId", value = "重点id", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "sysIndustryTypeInfoId", value = "行业id", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "SysAreaInfoId", value = "行政区划id", required = true, dataType = "String",paramType="query"),
    })
    @PostMapping(value = "add")
    public ResultBean<SysCompanyInfo> add(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "companyName");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.boolAdd(service.save(payload)) ;
        }
        return  result;
    }


    @ApiOperation(value = "更新企业信息", notes = "传入若干参数")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean update(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "company");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("company");
            if (map.containsKey("companyId")) {
                String id = map.get("companyId").toString();
                SysCompanyInfo entity = service.findId(id);
                boolean b = mapToObject.convertEntity(entity, map);
                if (entity != null) {
                    if (b) {
                        boolean a = service.update(entity);
                        if (a) {
                            return result.boolAdd("修改成功");
                        } else {
                            return result.boolAdd("该名称已存在");
                        }
                    } else {
                        return result.error("参数错误");
                    }
                } else {
                    return result.boolAdd("无此企业");
                }
            }else {
                return result.error("Id不能为空");
            }
        }
        return result;

    }


    /**
     * 通过id 查询企业信息
     * @param payload
     * @return
     */
    @ApiOperation(value="根据id 查询企业信息", notes="根据企业信息的id来获取企业信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParam(name = "id", value = "企业信息ID", required = true, dataType = "String", paramType = "query")
    @PostMapping(value = "info")
    public ResultBean<SysCompanyInfo> queryUser( @RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return  result.ok(service.findId(payload.get("id").toString()));
        }
        return  result;
    }

    /**
     * 根据id 删除企业信息
     * @param payload
     */
    @ApiOperation(value="删除企业信息", notes="根据id来指定企业信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParam(name = "id", value = "企业信息ID", required = true, dataType = "String",paramType = "query")
    @PostMapping(value = "/delete")
    public  ResultBean<Boolean> delete(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.bool(service.delete(payload.get("id").toString()));
        }
        return result;
    }

    /**
     * 分页查询用户列表
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页查询企业信息", notes="分页无参数查询企业信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
    })
    @PostMapping("paging")
    public ResultBean<Page<SysCompanyInfo>> showUsers(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            Page<SysCompanyInfo> pageSysCompanyInfo = service.findAll(Integer.parseInt(payload.get("pageIndex")
                    .toString()),Integer.parseInt(payload.get("pageSize").toString()));
            return result.ok(pageSysCompanyInfo);
        }
        return result;
    }

    /**
     * 分页查询用户列表
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页带参数查询企业信息", notes="分页带参数查询企业信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query")
    })
    @PostMapping("paging/query")
    public ResultBean<Page<SysCompanyInfo>> PageUsers(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize","condition");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            int pageIndex = Integer.parseInt(payload.get("pageIndex").toString());
            int pageSize = Integer.parseInt(payload.get("pageSize").toString());
            Map<String, Object> condition = (Map<String, Object>) payload.get("condition");
            Page<SysCompanyInfo> epaList = service.find(pageIndex, pageSize, condition);
            PageChunk<SysCompanyInfo> pageChunk = new PageChunk<>();
            if(epaList != null){
                return result.boolAdd(pageChunk.pageChunk(epaList));
            }else {
                return result.error("未查到企业列表");
            }
        }
        return result;
    }

}
